<template>
  <label
    class="os-switch"
    :class="{ 'is-checked': value }"
    @click="handleClick"
  >
    <span class="os-switch__core">
      <span class="os-switch__button"></span>
    </span>
  </label>
</template>

<script>
export default {
  name: "OsSwitch",
  props: {
    value: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    handleClick() {
      this.$emit("input", !this.value);
    },
  },
};
</script>

<style lang="scss">
.os-switch {
  display: inline-flex;
  align-items: center;
  position: relative;
  font-size: 14px;
  line-height: 20px;
  height: 20px;
  vertical-align: middle;
  .os-switch__core {
    margin: 0;
    display: inline-block;
    position: relative;
    width: 40px;
    height: 20px;
    border: 1px solid #dcdfe6;
    outline: none;
    border-radius: 10px;
    box-sizing: border-box;
    background: #dcdfe6;
    cursor: pointer;
    transition: border-color 0.3s, background-color 0.3s;
    vertical-align: middle;
    .os-switch__button {
      position: absolute;
      top: 1px;
      left: 1px;
      border-radius: 100%;
      transition: all 0.3s;
      width: 16px;
      height: 16px;
      background-color: #fff;
    }
  }
}
.os-switch.is-checked {
  .os-switch__core {
    border-color: #409eff;
    background-color: #409eff;
    .os-switch__button {
      transform: translateX(20px);
    }
  }
}
</style>
